package controller;

import format.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
    boolean result;
    ResponseResult responseResult=new ResponseResult();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        main(req,resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        main(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        main(req, resp);
    }

    private void main(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String URI=req.getRequestURI();
        System.out.println(URI);
        if(URI.contains("checkUser.action")){
            checkUser(req,resp);
        }else if(URI.contains("checkPhone")){
            checkPhone(req,resp);
        }else if(URI.contains("checkEmail.action")){
            checkEmail(req,resp);
        }
    }

    private void checkEmail(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String email=req.getParameter("email");
        String[] emails={"11@qq.com","11@163.com","11@126.com"};
       result= Arrays.toString(emails).contains(email);
        if(result){
            responseResult.setStatusNO(400);
            responseResult.setMessage("邮箱已注册");
            responseResult.setData("c重复了大哥");
            resp.getWriter().write(responseResult.toString());
        }else {
            responseResult.setStatusNO(200);
            responseResult.setMessage("该邮箱可用");
            responseResult.setData("邮箱  不错");
            resp.getWriter().write(responseResult.toString());
        }
    }

    private void checkPhone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String phone=req.getParameter("phone");
        String[] phones={"11","22","33"};
       result= Arrays.toString(phones).contains(phone);
        if(result){
            responseResult.setStatusNO(400);
            responseResult.setMessage("电话repeat");
            responseResult.setData("c重复了大哥");
            resp.getWriter().write(responseResult.toString());
        }else {
            responseResult.setStatusNO(200);
            responseResult.setMessage("电话ok");
            responseResult.setData("中  不错");
            resp.getWriter().write(responseResult.toString());
        }
    }

    private void checkUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String username=req.getParameter("username");

        System.out.println(username);
        String[] usernames={"张三","李四","王五"};
       result= Arrays.toString(usernames).contains(username);
        ResponseResult responseResult=new ResponseResult();
        if(result){
            responseResult.setStatusNO(400);
            responseResult.setMessage("名字repeat");
            responseResult.setData("c重复了大哥");
            resp.getWriter().write(responseResult.toString());
        }else {
            responseResult.setStatusNO(200);
            responseResult.setMessage("名字ok");
            responseResult.setData("中  不错");
            resp.getWriter().write(responseResult.toString());
        }
    }
}
